﻿"use strict";

function Ego(args)
{
    this.items = [];
    this.effects = [];
    this.group = [];
    Copy(this, args);
}

// Ego definition
Ego.prototype.name = 'Blank';
Ego.prototype.health = 10;
Ego.prototype.power = 2;
Ego.prototype.armor = 0;
Ego.prototype.magicArmor = 1;
Ego.prototype.waterResistance = 0;
Ego.prototype.fireResistance = 0;
Ego.prototype.airResistance = 0;
Ego.prototype.earthResistance = 0;
Ego.prototype.weapon = 'Fists';
Ego.prototype.group = [];
//determines strength category attacks damage
Ego.prototype.strength = 5;
//determines dexterity attacks
Ego.prototype.agility = 5;
//determines health
Ego.prototype.endurance = 5;
//determines magical damage
Ego.prototype.intelligence = 5;
//determines divine attacks
Ego.prototype.willpower = 5;
Ego.prototype.charisma = 5;
Ego.prototype.maxPower = 10;
Ego.prototype.currentPower = 10;
Ego.prototype.nextServerTick = 0;
Ego.prototype.inCombat = false;

//gets the attack, calculates damage with the benefits of the skill points, subtracts the cost from the player
Ego.prototype.Attack = function (enemy, attack)
{
    var bothalive = false;
    if (this.health <= 0)
        this.diedTo(enemy);
    else if (enemy.health <= 0)
        enemy.diedTo(this);
    else
        bothalive = true;
    if (!bothalive)
        return;
    var myattack = attacks[attack];
    var damage;
    //all run on one multiplier
    var skills = { physical: this.strength, magical: this.intelligence, dexterity: this.agility, divine: this.willpower, items: 0 };
    var damage = Math.max((myattack.damage - enemy.armor) + Math.round((.25 * skills[myattack.category])), 1);

    //if (myattack.category == 'physical') {var damage = Math.max((myattack.damage - enemy.armor) + Math.round((.25 * this.strength)), 1);}
    //if (myattack.category == 'magical') { var damage = Math.max((myattack.damage - enemy.armor) + Math.round((.25 * this.intelligence)), 1); }
    //if (myattack.category == 'dexterity') { var damage = Math.max((myattack.damage - enemy.armor) + Math.round((.25 * this.agility)), 1); }
    //if (myattack.category == 'divine') { var damage = Math.max((myattack.damage - enemy.armor) + Math.round((.25 * this.willpower)), 1); }

    if (this.currentPower - myattack.cost < 0)
    {
        log(this.name + ' doesn\'t have enough power to cast ' + attack + '!');
        return;
    }
    this.currentPower -= myattack.cost;
    //var damage = Math.max((myattack.damage - enemy.armor), 1);
    enemy.health = enemy.health - damage;
    log(this.name + ' ' + myattack.text + ' for ' + damage + ' damage.' + ' ' + this.name + ' has ' + this.currentPower + ' power left, ' + enemy.name + ' has ' + enemy.health + ' health left.');
}

//ticks effects ( ie poison, fire, acid) not tested or implemented
Ego.prototype.TickEffects = function (enemy)
{
    // Tick effects

    if (this.health <= 0)
    {
        this.diedTo(enemy);
        return;
    }

    log(this.name + " has " + this.health + " hp left");
}
//called when something dies, takes enemy as variable, works for both player and monster
Ego.prototype.diedTo = function (enemy)
{
    //log(this.name + ' is dead!');
    for (var i = 0; i < this.items.length; i++)
    {
        enemy.items.push(this.items[i]);
        log(this.name + ' dropped ' + this.items[i] + '!');
    }
    //for (var i = 0; i < this.items.length; i++) { log('P1 ' + this.items[i]); }
}
//ticks the "server", causing players and monsters to regen power
Ego.prototype.serverTick = function ()
{
    if (engine.now < this.nextServerTick)
    {
        return;
    }
    this.nextServerTick = engine.now + 5000;
    var adj = 0;
    if (this.inCombat == true)
    {
        adj = 1;
    }
    else
    {
        adj = 2;
    }
    this.currentPower = Math.min(this.currentPower + adj, this.calcMaxPower());
}
//calculates the max power of the player, should be called on stat augmenting effects (ie potions, poisons, blessings) and when the player levels up 
Ego.prototype.calcMaxPower = function ()
{
    var maxSkill = 0;
    var subSkill = 0;
    var skills = ['strength', 'agility', 'endurance', 'intelligence', 'willpower']

    for (var i = 0; i < skills.length; i++)
    {
        if (this[skills[i]] > maxSkill) maxSkill = this[skills[i]];
        subSkill += this[skills[i]];
    }

    subSkill = (subSkill - maxSkill) * .25;
    var retval = maxSkill + subSkill;
    return retval;
}
//monster definition
function Monster(args) { Ego.call(this, args); }
Monster.prototype = new Ego();
Monster.prototype.name = 'Monster';
Monster.prototype.attacks = ['Fireball', 'Basic Attack', 'Weapon Attack'];

//player character definitions ( should be able to work for a party
function Player(args) { Ego.call(this, args); }
Player.prototype = new Ego();
Player.prototype.name = 'Player'
Player.prototype.armor = 1;
Player.prototype.weapon = 'Sword';
Player.prototype.charisma = 10;
Player.prototype.intelligence = 10;
Player.prototype.strength = 10;
